package cn.oms.manage.dao.persist.repository.impl;

import cn.oms.commons.pojo.vo.PageData;
import cn.oms.commons.util.PageInfoToPageDataConverter;
import cn.oms.manage.dao.persist.mapper.MedicineMapper;
import cn.oms.manage.dao.persist.repository.IMedicineRepository;
import cn.oms.manage.pojo.entity.Medicine;
import cn.oms.manage.pojo.vo.MedicineListVO;
import cn.oms.manage.pojo.vo.MedicineStandardVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Slf4j
@Repository
public class MedicineRepositoryImpl implements IMedicineRepository {

    @Autowired
    private MedicineMapper medicineMapper;

    public MedicineRepositoryImpl() {
        log.debug("创建数据访问实现类对象：MedicineRepositoryImpl");
    }

    @Override
    public int insert(Medicine medicine) {
        log.debug("开始执行【插入订单】的数据访问，参数：{}", medicine);
        return medicineMapper.insert(medicine);
    }

    @Override
    public int deleteById(Long id) {
        log.debug("开始执行【根据ID删除药品】的数据访问，参数：{}", id);
        return medicineMapper.deleteById(id);
    }

    @Override
    public int update(Medicine medicine) {
        log.debug("开始执行【更新药品】的数据访问，参数：{}", medicine);
        return medicineMapper.updateById(medicine);
    }

    @Override
    public int countByName(String name) {
        log.debug("开始执行【统计匹配名称的药品的数量】的数据访问，参数：{}", name);
        QueryWrapper<Medicine> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", name);
        return medicineMapper.selectCount(queryWrapper);
    }

    @Override
    public MedicineStandardVO getStandardById(Long id) {
        log.debug("开始执行【根据ID查询药品信息】的数据访问，参数：{}", id);
        return medicineMapper.getStandardById(id);
    }

    @Override
    public int countByNameAndNotId(Long id, String name) {
        log.debug("开始执行【统计匹配ID但不匹配名称的药品的数量】的数据访问，ID：{}，名称：{}", id, name);
        QueryWrapper<Medicine> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", name).ne("id", id);
        return medicineMapper.selectCount(queryWrapper);
    }

    @Override
    public PageData<MedicineListVO> listByCategoryId(Long categoryId, Integer pageNum, Integer pageSize) {
        log.debug("开始执行【根据属性模板查询属性列表】的数据访问，页码：{}，每页记录数：{}", pageNum, pageSize);
        PageHelper.startPage(pageNum, pageSize);
        List<MedicineListVO> list = medicineMapper.listByCategoryId(categoryId);
        PageInfo<MedicineListVO> pageInfo = new PageInfo<>(list);
        PageData<MedicineListVO> pageData = PageInfoToPageDataConverter.convert(pageInfo);
        return pageData;
    }


}
